<style type="text/css">
table.gridtable {
	font-family: verdana,arial,sans-serif;
	font-size:11px;
	color:#333333;
	border-width: 1px;
	border-color: #666666;
	border-collapse: collapse;
}
table.gridtable th {
	border-width: 1px;
	padding: 2px;
	margin: 2px;
	border-style: solid;
	border-color: #666666;
	background-color: #dedede;
}
table.gridtable td {
	border-width: 1px;
	padding: 2px;
	margin: 2px;
	border-style: solid;
	border-color: #666666;
	background-color: #ffffff;
}
input[type=text] { border:none;padding:0px;margin:0px}
</style>
<?php
$database = 'd0186e70';
$mysqli = new mysqli("localhost", "d0186e70", "test123", $database);
if ($mysqli->connect_errno) {
    echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}

	print("<table class='gridtable'>");
	print("<th colspan='2'>NAME</th>");
	print("<th>SCORE</th>");
	print("<form action='data2.php'>");
	$res = $mysqli->query("select * from players order by firstName");
	while($row=$res->fetch_assoc()) {
		$resCount = $mysqli->query("select count(*) count from game_data where player_id =".$row['id']." and score!=-1");
		$rowCount = $resCount->fetch_assoc();
		$count = $rowCount['count'];
		print("<tr>");
		print("<td>".$row['firstName']." ".$row['lastName']."</td>");
		print("<td>".$count."</td>");
		print("<td>");
		print("<input type='text' name='player_".$row['id']."' size='1' />");
		print("</td>");
		print("</tr>");
		
	}
	print("<tr>");
	print("<td>");
	$res = $mysqli->query("select distinct g.id, g.game_name, count(gd.id) count from game_data gd, games g where gd.game_id = g.id group by g.id, g.game_name, gd.game_id order by count desc");
	print("<select name='game_id' size='1'>");
	while($row=$res->fetch_assoc()) {
		//print("<option value=".$row['id'].">".$row['game_name']." (".$row['count'].")</option>");
		print("<option value=".$row['id'].">".$row['game_name']."</option>");
	}
	print("</select>");
	print("</td>");
	print("<td colspan='2'>");
	print("<input type='submit' name='savegame' value='Save'/>");
	print("</td>");
	print("</tr>");
	print("</form>");
	print("</table></br>");
	
	if (isset($_GET['savegame']) == true) {
		$res = $mysqli->query("select max(id) max_game_id from game_data");
		$row = $res->fetch_assoc();
		$id = $row['max_game_id'] + 1;
		foreach($_GET as $key => $value) {
			if (startsWith($key,'player_') && $value != "") {
				$player_id = explode("_", $key)[1];
				//echo $player_id;
				$sql = "INSERT INTO game_data (id,date, game_id, player_id, score) VALUES (".$id.",CURDATE(), ".$_GET['game_id'].",".$player_id.",".$value.")";
				//echo $sql;
				mysqli_query($mysqli,$sql);
				$mysqli->commit();
				//echo "\$_GET['$key'] = $value<br/>";
			}
		}
	}
	
	$res = $mysqli->query("select * from view_all_kw");

	print("<form action='data2.php'>");
	print("<select onchange='this.form.submit()' name='kw' size='1'>");
	while($row=$res->fetch_assoc()) {
		if($row['kw2'] == $_GET['kw']) {
			print("<option selected value=".$row['kw2'].">".$row['kw']."</option>");
		} else {
			print("<option value=".$row['kw2'].">".$row['kw']."</option>");
		}
	}
	print("</select>");
	print("</form>");
	
	$kw = date('Y');
	$year = date('W');
	if (isset($_GET['kw']) == true) 
	{
		$yearkw = $_GET['kw'];

	} else {
		$yearkw = date('Y').date('W');
	}
	
	print("<table class='gridtable'>");
	print("<th>KW ".$year."-".$kw."</th>");
	print("<th>SCORE</th>");
	print("<th>COUNT</th>");
	$res = $mysqli->query("select * from view_score_kw_winner where yearkw=".$yearkw);
	while($row=$res->fetch_assoc()) {
		print("<tr>");
		print("<td>");
		print($row['firstName']." ".$row['lastName']);
		print("</td>");
		print("<td>");
		print(round($row['score_abs'],3));
		print("</td>");
		print("<td>");
		print($row['count_games']);
		print("</td>");
		print("</tr>");
	}
	print("</table></br>");

	/*foreach($_GET as $key => $value) {
		echo "\$_GET['$key'] = $value<br/>";
	}*/
	
	/*print("<table border='1'>");
		$kw = "";
		$res = $mysqli->query("select * from view_score_kw_winner order by year desc,kw desc, score_abs desc");
		while($row=$res->fetch_assoc()) {
		if($kw != $row['kw'] && $kw != "") {
				print("</tr></table><table border='1'><tr>");
				$kw = $row['kw'];
			}
			print("<tr>");
			print("<td>");
			print($row['year']);
			print("</td>");
			print("<td>");
			print($row['kw']);
			print("</td>");
			print("<td>");
			print($row['contraction']);
			print("</td>");
			print("<td>");
			print(round($row['score_abs'],3));
			print("</td>");
			print("<td>");
			print($row['count_games']);
			print("</td>");
			print("</tr>");
			
			$kw = $row['kw'];
			
			
		}
		print("</table>");
	*/
	function startsWith($haystack, $needle)
	{
		return $needle === "" || strpos($haystack, $needle) === 0;
	}
	function endsWith($haystack, $needle)
	{
		return $needle === "" || substr($haystack, -strlen($needle)) === $needle;
	}
?>